<?
require_once 'BaseAction.php';

class UserController extends TT_Base_Action
{    
	public function indexAction()
	{
		exit();
	}
	
	private function _unpack($obj) {
      if ($obj->data && $data = unserialize($obj->data)) 
      {
        foreach ($data as $key => $value) {
          if (!isset($obj->$key)) {
            $obj->$key = $value;
          }
        }
      }
      return $obj;
    }
	
	//Thuc hien dang nhap dua vao username va password.
	//Tra ve true neu dang nhap thanh cong
	//Tra ve false neu dang nhap that bai
	private function _doLogin($username, $password, $remember){
	
		//Kiem tra username va password trong database
		$u_model = $this->loadModel("Users");
		$user = $u_model->checkUser($username, $password);
		
		//Neu username va password hop le, cho nguoi dung dang nhap
		if ($user){
			//Convert du lieu user sang dang key->value de luu vao registry
			$user = $this->_unpack($user);
            
            //set cookie thanh "da dang nhap" neu nguoi dung chon ghi nho'
            if (isset($remember) && $remember == 1){
                $cookieParams = session_get_cookie_params();
                session_set_cookie_params(
                    TAMTAY_SESSION_TTL,
                    $cookieParams['path'],
                    $cookieParams['domain'],
                    $cookieParams['secure']
                );
            }
            
            $sess = Zend_Session::getSaveHandler();
            $sess->regenerateSessionID();
            
            //Luu tru thong tin ve nguoi dung vao registry de dung lai lan sau
            Zend_Registry::set('user', $user);   
			
			return true; // tra ve dang nhap thanh cong
		}
		else
            $this->view->thongbao = 1;
			return false; //dang nhap that bai
	}
	
    public function loginAction()
    {
        $u = $this->getUser();

        if ($u->uid > 0){ //neu $u->uid > 0 tuc la user da login
            
			$this->view->user = $u; //truyen tham so cho View de hien thi ten nguoi dung.
            
            $m_model = $this->loadModel('Modes');  
            $num=$m_model->showoverdue_num();
            $this->view->num=$num[0]->num;
            
            $num2=$m_model->showmaturity_num();
            $this->view->num2=$num2[0]->num;            
            return; 
        }
		
        if ($this->getRequest()->isPost()){ //neu nguoi dung submit form dang nhap
		
            $username = trim($this->_getParam('username'));
			
            $password = $this->_getParam('password');
            $password = md5($password);
			
            $remember = $this->_getParam('saveme', 0);      
			
            $is_logged_in = $this->_doLogin($username, $password, $remember);
			
            if ( ! $is_logged_in){                                                			
                $this->view->is_login_fail = true;
				$this->view->username = $username;
            } else {
				$u = $this->getUser();
				$this->view->user = $u;
            }
        }

    }
  
    // Thay doi password
    public function passwordAction()
    {
        // Tham so
        $m_model = $this->loadModel('Modes');  
        $num1=$m_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$m_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $u = $this->getUser();
        if($u->uid == 0){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }      
        
        $this->view->user = $u;
        $uid=$u->uid;
        $this->view->uid = $uid;
        if($this->getRequest()->isPost()){
            $password_old = $this->_getParam('password_old');
            $password_old = md5($password_old);
            $password_new = $this->_getParam('password_new');
            $password_new = md5($password_new);
            $password = $this->_getParam('password');
            $password = md5($password);
            $u_model = $this->loadModel("Users");
           if($password_old == $u->pass)
            {
                if($password == $password_new){
                    $a = $u_model->setPass($uid,$password);
                    $flag_pas = 0;
                }else{
                    $flag_pas = 2;
                }
            }else{
                $flag_pas = 1;
            }
            $this->view->flag_pas=$flag_pas;
            
        }
    }
    
    // Hien thi thong tin vay
    public function infoAction()
    {
        // Tham so
        $m_model = $this->loadModel('Modes');  
        $num1=$m_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$m_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        if($u->uid == 0){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }
        //$opt=$params['opt'];
        //if($opt!="")
        //{
            //$link="user/info?page=1&select=".$opt;
             //$this->_helper->getHelper('Redirector')->gotoUrl($link);
        //}                     
        
        $this->view->user = $u;
        $uid = $u->uid;
        $this->view->test=$uid;
        
        $u_model = $this->loadModel('Users');
        $username = $u_model->checkLoan($uid);
        $this->view->info = $username;   
        
        // Show tat ca ra
        /*
        $showall = $u_model->showall();
        $this->view->showall=$showall;
        */ 
        $showall = $u_model->showall2($page_start,$page_rows);
        $this->view->showall=$showall;
        $num=$u_model->showall2_num();
        $this->view->num=$num;       
    }
  
	public function logoutAction()
	{
		$u = $this->getUser();
		if ($u->uid == 0){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
		}                    
		Zend_Session::destroy();
		$sess = Zend_Session::getSaveHandler();         		
		$u = $sess->getAnonymousUser();
		Zend_Registry::set('user', $u);
		$ref = $_SERVER['HTTP_REFERER'];
		if ($ref != ''){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit($ref);
		} else {
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
		}
	}
}
?>